//
//  UPUserDefaults.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/16.
//

#import "UPUserDefaults.h"

NSString *const loginSessionIdKey = @"loginSessionIdKey";
NSString *const loginPhoneNumberKey = @"loginPhoneNumberKey";
NSString *const environmentCountryKey = @"environmentCountryKey";
NSString *const myAddressKey = @"myAddressKey";

NSString *const loginStartTimeKey = @"loginStartTimeKey";
NSString *const loginEndTimeKey = @"loginEndTimeKey";
NSString *const loginReportGoogleMarketKey = @"loginReportGoogleMarketKey";
NSString *const homeReportGoogleMarketKey = @"homeReportGoogleMarketKey";

@implementation UPUserDefaults

#pragma mark - 基础方法
+ (void)setObject:(id)object forKey:(NSString *)key {
    if (key) {
        [[NSUserDefaults standardUserDefaults] setObject:object forKey:key];
    }
}

+ (id)objectForKey:(NSString *)key {
    return key ? [[NSUserDefaults standardUserDefaults] objectForKey:key] : nil;
}

+ (void)removeObjectForKey:(NSString *)key {
    if (key) {
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
    }
}

+ (BOOL)hasValueForKey:(NSString *)key {
    return key ? ([[NSUserDefaults standardUserDefaults] objectForKey:key] != nil) : NO;
}

#pragma mark - Bool 相关
+ (void)setBool:(BOOL)value forKey:(NSString *)key {
    if (key) {
        [[NSUserDefaults standardUserDefaults] setBool:value forKey:key];
    }
}

+ (BOOL)boolForKey:(NSString *)key defaultValue:(BOOL)defaultValue {
    return key ? [[NSUserDefaults standardUserDefaults] boolForKey:key] : defaultValue;
}

+ (BOOL)boolForKey:(NSString *)key {
    return [self boolForKey:key defaultValue:NO];
}

#pragma mark - Integer 相关
+ (void)setInteger:(NSInteger)value forKey:(NSString *)key {
    if (key) {
        [[NSUserDefaults standardUserDefaults] setInteger:value forKey:key];
    }
}

+ (NSInteger)integerForKey:(NSString *)key defaultValue:(NSInteger)defaultValue {
    return key ? [[NSUserDefaults standardUserDefaults] integerForKey:key] : defaultValue;
}

+ (NSInteger)integerForKey:(NSString *)key {
    return [self integerForKey:key defaultValue:0];
}

#pragma mark - Float 相关
+ (void)setFloat:(float)value forKey:(NSString *)key {
    if (key) {
        [[NSUserDefaults standardUserDefaults] setFloat:value forKey:key];
    }
}

+ (float)floatForKey:(NSString *)key defaultValue:(float)defaultValue {
    return key ? [[NSUserDefaults standardUserDefaults] floatForKey:key] : defaultValue;
}

+ (float)floatForKey:(NSString *)key {
    return [self floatForKey:key defaultValue:0.0f];
}

#pragma mark - Double 相关
+ (void)setDouble:(double)value forKey:(NSString *)key {
    if (key) {
        [[NSUserDefaults standardUserDefaults] setDouble:value forKey:key];
    }
}

+ (double)doubleForKey:(NSString *)key defaultValue:(double)defaultValue {
    return key ? [[NSUserDefaults standardUserDefaults] doubleForKey:key] : defaultValue;
}

+ (double)doubleForKey:(NSString *)key {
    return [self doubleForKey:key defaultValue:0.0];
}

#pragma mark - String 相关
+ (void)setString:(NSString *)value forKey:(NSString *)key {
    [self setObject:value forKey:key];
}

+ (NSString *)stringForKey:(NSString *)key defaultValue:(NSString *)defaultValue {
    NSString *value = [self objectForKey:key];
    return [value isKindOfClass:[NSString class]] ? value : defaultValue;
}

+ (NSString *)stringForKey:(NSString *)key {
    return [self stringForKey:key defaultValue:nil];
}

#pragma mark - URL 相关
+ (void)setURL:(NSURL *)url forKey:(NSString *)key {
    if (key) {
        [[NSUserDefaults standardUserDefaults] setURL:url forKey:key];
    }
}

+ (NSURL *)URLForKey:(NSString *)key defaultValue:(NSURL *)defaultValue {
    return key ? [[NSUserDefaults standardUserDefaults] URLForKey:key] : defaultValue;
}

+ (NSURL *)URLForKey:(NSString *)key {
    return [self URLForKey:key defaultValue:nil];
}

#pragma mark - 便捷方法
+ (void)clearAllData {
    NSString *appDomain = [[NSBundle mainBundle] bundleIdentifier];
    [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (void)synchronize {
    [[NSUserDefaults standardUserDefaults] synchronize];
}

@end
